extends Node
class_name SlimeSkill

@export var slime: Slime
@onready var body: Node2D = %body
@onready var slime_animations: SlimeAnimations = %SlimeAnimations
@onready var animation_player: AnimationPlayer = %AnimationPlayer
@export var enemy_hit_box_component: EnemyHitBoxComponent

## 初始速度
var init_speed: float
## 初始攻击前摇
var init_attack_delay: float
var init_size: float
var timer: Timer

## 冲刺砍击相关
var is_dashing: bool = false
var dash_duration: float = 0.3
var dash_damage_multiplier: float = 1.5
var dash_attack_chance: float = 0.25  # 25%概率触发冲刺攻击

func _ready() -> void:
	await owner.ready
	init_speed = slime.slime_stats.get_float("speed")
	init_attack_delay = slime.slime_stats.get_float("attack_delay")
	init_size = enemy_hit_box_component.length

## 冲刺砍击攻击
func dash_attack() -> bool:
	slime.attack_vfx.look_at(Utils.player.global_position)
	enemy_hit_box_component.look_at(Utils.player.global_position)
	# 25%概率触发冲刺攻击
	if randf() < dash_attack_chance:
		_start_dash_attack()
		return true
	else:
		# 普通挥砍攻击
		_normal_attack()
		return false

## 开始冲刺攻击
func _start_dash_attack() -> void:
	is_dashing = true
	animation_player.queue("attack1")  # 播放普通挥砍动画
	
	# 冲刺移动
	var dash_tween = create_tween()
	var dash_direction = (Utils.player.global_position - slime.global_position).normalized()
	var dash_distance = slime.get_stats().get_float("speed") * 4 * dash_duration
	
	dash_tween.tween_method(_update_dash_position, slime.global_position, 
		slime.global_position + dash_direction * dash_distance, dash_duration)
	
	# 冲刺结束后
	await dash_tween.finished
	is_dashing = false
	
## 更新冲刺位置
func _update_dash_position(new_position: Vector2) -> void:
	slime.global_position = new_position

## 普通挥砍攻击
func _normal_attack() -> void:
	animation_player.queue("attack2")  # 播放普通挥砍动画

